package kr.kimkisik.middle1math.gyesan;

import kr.kimkisik.middle1math.R;
import kr.kimkisik.middle1math.danwon.DanwonMain;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.nbpcorp.mobilead.sdk.MobileAdListener;
import com.nbpcorp.mobilead.sdk.MobileAdView;

public class GCD3numMain extends Activity implements MobileAdListener {
	private MobileAdView adView = null;
	TextView mResult;
	EditText mLeft;
	EditText mRight, mRight2;
	int mL = 0;
	int mR = 0, mR2 = 0;
	InputMethodManager imm;
	Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.gyesan_3greatcommondivisor);

		mResult = (TextView) findViewById(R.id.great3commondivisor_result);
		mLeft = (EditText) findViewById(R.id.great3commondivisor_left);
		mRight = (EditText) findViewById(R.id.great3commondivisor_right);
		mRight2 = (EditText) findViewById(R.id.great3commondivisor_rigth2);
		button = (Button) findViewById(R.id.great3commondivisor_gyesangogo);
		adView = (MobileAdView) findViewById(R.id.adview1);
		adView.setListener(this);
		adView.start();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (adView != null) {
			adView.destroy(); // 10
			adView = null;
		}
	}

	@Override
	public void onReceive(int err) {
		// event for receive ad
	}

	public static int GCD(int a, int b) {
		while (b != 0) {
			int temp = a % b;
			a = b;
			b = temp;
		}
		return Math.abs(a);
	}

	public static int GCD2(int a, int b) {
		a = GCD(a, b);
		while (b != 0) {
			int temp2 = a % b;
			a = b;
			b = temp2;
		}
		return Math.abs(a);

	}

	public void mOnClick(View v) {
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		try {
			mL = Integer.parseInt(mLeft.getText().toString());
			mR = Integer.parseInt(mRight.getText().toString());
			mR2 = Integer.parseInt(mRight2.getText().toString());
		} catch (Exception e) {
			Toast.makeText(this, "숫자를 입력하지 않으셨거나, \n 잘못입력하셨습니다.",
					Toast.LENGTH_SHORT).show();
		}
		switch (v.getId()) {
		case R.id.great3commondivisor_gyesangogo:
			if (mL == 0 || mR == 0 || mR2 == 0) {
				Toast.makeText(this, "숫자를 입력하지 않으셨거나, \n 잘못입력하셨습니다.",
						Toast.LENGTH_SHORT).show();
			} else {
				imm.hideSoftInputFromWindow(button.getWindowToken(), 0);
				mResult.setText("▶ 입력하신 세 수 : " + mLeft.getText() + ", "
						+ mRight.getText() + ", " + mRight2.getText()
						+ "\n☞ 최대 공약수 : " + GCD2(GCD(mL, mR), mR));
				mResult.setTextColor(Color.WHITE);
			}
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent();
		switch (item.getItemId()) {

		case R.id.danwonmenu:
			intent.setClass(this, DanwonMain.class);
			startActivity(intent);
			return true;

		case R.id.gyesan:
			intent.setClass(this, GyesanMain.class);
			startActivity(intent);
			return true;

		}
		return false;
	}

}
